

package cn.com.w.simple.demo.vo;

import java.util.ArrayList;
import java.util.List;

public class RequestResult<T> {
    private static final String OK_STR = "ok";
    public static final RequestResult<String> OK = new RequestResult("ok");
    private int count;
    private String message;
    private List<ErrorInfo> errors;
    private T data;

    private RequestResult(String message) {
        this.message = message;
    }

    public boolean isSuccess() {
        return this.message == null || "ok".equals(this.message);
    }

    public static <T> RequestResult<T> ok() {
        return new RequestResult("ok");
    }

    public static <T> RequestResult<T> ok(T data) {
        RequestResult<T> result = new RequestResult("ok");
        result.data = data;
        return result;
    }

    public static <T> RequestResult<T> ok(T data, int count) {
        RequestResult<T> result = new RequestResult("ok");
        result.data = data;
        result.count = count;
        return result;
    }

    public static RequestResult error(String message) {
        RequestResult result = new RequestResult(message);
        result.data = message;
        return result;
    }

    public int getCount() {
        return this.count;
    }

    public T getData() {
        return this.data;
    }

    public String getMessage() {
        return this.message;
    }

    public List<ErrorInfo> getErrors() {
        return this.errors;
    }

    public RequestResult<T> addErrors(ErrorInfo error) {
        if (this.errors == null) {
            this.errors = new ArrayList(1);
        }

        this.errors.add(error);
        return this;
    }

    public RequestResult<T> addErrors(String code, String resource, String field) {
        if (this.errors == null) {
            this.errors = new ArrayList(1);
        }

        this.errors.add(new ErrorInfo(code, resource, field));
        return this;
    }

    public static class ErrorInfo {
        private String resource;
        private String field;
        private String code;

        public ErrorInfo(String code, String resource, String field) {
            this.resource = resource;
            this.field = field;
            this.code = code;
        }

        public String getResource() {
            return this.resource;
        }

        public String getField() {
            return this.field;
        }

        public String getCode() {
            return this.code;
        }
    }
}
